# from selenium.webdriver.edge.options import Options
import sys

from selenium.webdriver.firefox.options import Options
from bs4 import BeautifulSoup
from selenium.webdriver.support import expected_conditions
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By


# class WeatherSpider:
#     url = 'https://tianqi.qq.com/'
#     selector = '#txt-name'
#
#     def __init__(self):
#         pass
#     def parse_weather(self):
#         url = 'https://tianqi.qq.com/'
#         selector = '#txt-name'
#         option = Options()
#         option.add_argument('headless')
#         # option.add_argument('user-agent="MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"')
#         browser = webdriver.Edge(options=option)
#         browser.get(self.url)
#         # WebDriverWait(browser, 2).until(
#         # expected_conditions.presence_of_element_located((By.CSS_SELECTOR, self.selector))
#         # )
#         html = browser.page_source
#         browser.quit()
#         if html:
#             soup = BeautifulSoup(html, 'lxml')
#             raw_data = soup.select_one(self.selector).text
#         return raw_data


def parse_weather():
    ''' 获取天气信息'''
    url = 'https://tianqi.qq.com/'
    temperature = None
    w_name = None
    try:
        txt_name = '#txt-name'
        txt_temperature = '#txt-temperature'

        if sys.platform=='linux':
            options = webdriver.FirefoxOptions()
            options.add_argument('-headless')
            browser = webdriver.Firefox(options=options)
        else:
            options = webdriver.EdgeOptions()
            options.add_argument('headless')
            browser = webdriver.Edge(options=options)
        browser.get(url)
        html = browser.page_source
        browser.quit()
        if html:
            soup = BeautifulSoup(html, 'lxml')
            w_name = soup.select_one(txt_name).text
            temperature = soup.select_one(txt_temperature).text
    except Exception as e:
        raise e
        print('获取天气信息失败！',e)
    return w_name, temperature


if __name__ == '__main__':
    tname, temp = parse_weather()
    print(tname, temp)
